package com.gss.plugin.oauth2.exception;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author DYang
 * @date 2023/7/21
 * oauth2 异常翻译
 */
@Slf4j
@JsonSerialize(using = OAuth2ExceptionSerializer.class)
public class OAuth2TransException extends OAuth2Exception {

    /**
     * OAuth2Exception
     */
    public static final String INVALID_REQUEST = "invalid_request";
    public static final String INVALID_CLIENT = "invalid_client";
    public static final String INVALID_GRANT = "invalid_grant";
    public static final String UNAUTHORIZED_CLIENT = "unauthorized_client";
    public static final String UNSUPPORTED_GRANT_TYPE = "unsupported_grant_type";
    public static final String INVALID_SCOPE = "invalid_scope";
    public static final String INSUFFICIENT_SCOPE = "insufficient_scope";
    public static final String INVALID_TOKEN = "invalid_token";
    public static final String REDIRECT_URI_MISMATCH ="redirect_uri_mismatch";
    public static final String UNSUPPORTED_RESPONSE_TYPE ="unsupported_response_type";
    public static final String ACCESS_DENIED = "access_denied";
    /**
     * 其他异常
     */
    public static final String METHOD_NOT_ALLOWED = "method_not_allowed";
    public static final String SERVER_ERROR = "server_error";
    public static final String UNAUTHORIZED = "unauthorized";

    private static Map<String, String> oAuth2ErrorMap = new ConcurrentHashMap<>();

    /**
     * 映射了一部分
     */
    static {
        oAuth2ErrorMap.put(INVALID_REQUEST,"无效的请求");
        oAuth2ErrorMap.put(INVALID_CLIENT,"无效的客户端");
        oAuth2ErrorMap.put(INVALID_GRANT,"无效的授权模式");
        oAuth2ErrorMap.put(INVALID_SCOPE,"权限不足");
        oAuth2ErrorMap.put(UNSUPPORTED_GRANT_TYPE,"不支持的授权模式类型");
        oAuth2ErrorMap.put(UNAUTHORIZED_CLIENT,"未认证请求");
        oAuth2ErrorMap.put(ACCESS_DENIED,"拒绝访问");
        oAuth2ErrorMap.put(INSUFFICIENT_SCOPE,"不存在scope");
        oAuth2ErrorMap.put(INVALID_TOKEN,"TOKEN失效");
        oAuth2ErrorMap.put(REDIRECT_URI_MISMATCH,"未找到重定向请求");
        oAuth2ErrorMap.put(UNSUPPORTED_RESPONSE_TYPE,"不支持的响应类型");

        oAuth2ErrorMap.put(METHOD_NOT_ALLOWED,"方法不允许访问");
        oAuth2ErrorMap.put(SERVER_ERROR,"服务器内部异常");
        oAuth2ErrorMap.put(UNAUTHORIZED,"未授权");
    }

    /**
     * oAuth2ErrorCode的扩展信息
     */
    private String myExtendMessage;

    public OAuth2TransException(String msg, Throwable t) {
        super(msg, t);
    }

    public OAuth2TransException(String msg, String oAuth2ErrorCode, Throwable t) {
        super(msg, t);
        t.printStackTrace();
        String oAuth2ErrorMessage = oAuth2ErrorMap.get(oAuth2ErrorCode);
        this.myExtendMessage = oAuth2ErrorMessage != null ? oAuth2ErrorMessage : "未知异常：" + oAuth2ErrorCode;
    }

    public String getMyExtendMessage() {
        return myExtendMessage;
    }

    public void setMyExtendMessage(String myExtendMessage) {
        this.myExtendMessage = myExtendMessage;
    }
}
